# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pandas as pd

class ScrapyVip40Pipeline:
    def __init__(self):
        # 列名与items中的名字一一对应，名字不能错，如果错，会以items中的名字为准传输数据
        self.df = pd.DataFrame(columns=['标题','链接','原价','售价','折扣','商品属性','品牌商','标签'])

    def process_item(self, item, spider):
        # 防止label为空
        item['标签'] = item.get('标签','')
        series = pd.Series(item)
        self.df = self.df.append(series,ignore_index=True)

        return item

    def close_spider(self,spider):
        self.df.to_excel('goodsData.xlsx',index=False,sheet_name='唯品会商品数据')
